package com.gem.shop.dao.impl;

import com.gem.shop.dao.CustomerDao;
import com.gem.shop.entity.Customer;
import com.gem.shop.util.HibernateTemplate;

/**
 * @author zhang
 * @date 2020/9/16 18:54
 */
public class CustomerDaoImpl implements CustomerDao {
    @Override
    public void insertCustomer(Customer customer) {
        HibernateTemplate.execute(ses -> ses.save(customer));
    }

    @Override
    public void updateCustomer(Customer customer) {
        HibernateTemplate.execute(ses -> {ses.update(customer);
        return null;});
    }

    @Override
    public Customer selectCustomerByUsernameAndPassword(String username, String password) {
        return (Customer) HibernateTemplate.execute(ses -> ses.createQuery("from Customer where username = :username and password = :password").setParameter("username",username).setParameter("password",password).uniqueResult());
    }

    @Override
    public Customer selectCustomerByName(String username) {
        return (Customer) HibernateTemplate.execute(ses -> ses.createQuery("from Customer where username = :username").setParameter("username",username).uniqueResult());
    }

    @Override
    public Customer selectCustomerById(int id) {
        return (Customer) HibernateTemplate.execute(ses -> ses.get(Customer.class,id));
    }
}
